package com.rabbiter.si.controller;

import com.rabbiter.si.service.StudentService;
import com.rabbiter.si.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/")
public class LogOutController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/loginout")
    void handleLogout(@RequestBody Map<String, Object> requestBody) {
        // 从JSON请求体中获取参数
        String type = (String) requestBody.get("type");
        Object userIdObj = requestBody.get("userId");


        // 转换userId为整数
        Integer userId = null;

        if (userIdObj instanceof String) {
            userId = Integer.parseInt((String) userIdObj);
        } else if (userIdObj instanceof Number) {
            userId = ((Number) userIdObj).intValue();
        }
        // 业务逻辑处理
        System.out.println("接收到登出请求 - type: " + type + ", userId: " + userId);

        if ("student".equals(type)) {
            studentService.updateLastLogOutTimeAndStatus(userId);
        }
        else  {
            teacherService.updateLastLogOutTimeAndStatus(userId);
        }


    }
}

